<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/8
 * Time: 10:32
 */
namespace App\FreeBird\Models;
use App\Base\Models\AreaModel;
use App\Base\Models\BaseModel;
use App\Weixin\Models\WeixinUserModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class StoreUserModel extends BaseModel
{
    use SoftDeletes;
    /**
     * 表名
     * @var string
     */
    public $table="freebird_store_user";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps=true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';

    /**
     * 删除
     */
    const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';

    /**
     * 自动添充值属性
     * @var array
     */
//    protected $attributes = [
//        'uid' => 11,
//    ];

    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = ['status','create_time','update_time','delete_time'];



    protected $appends = ["country_name", "city_name","region_name"];


    /**
     * 地址模型
     * @return AreaModel
     */
    public function getAreaModel(){
        return app()->make(AreaModel::class);
    }

    /**
     * 完成订单
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user(){
        return $this->hasOne(WeixinUserModel::class,"uid","uid");
    }
    /**
     * 省份 名称
     * @return string
     */
    public function getCountryNameAttribute(){
        $area_id=$this->country;
        $info=$this->getAreaModel()->newQuery()->where("area_id",$area_id)->first();
        return $info["title"] ?? '';
    }
    /**
     * 城市 名称
     * @return string
     */
    public function getCityNameAttribute(){
        $area_id=$this->city;
        $info=$this->getAreaModel()->newQuery()->where("area_id",$area_id)->first();
        return $info["title"] ?? '';
    }
    /**
     * 区 名称
     * @return string
     */
    public function getRegionNameAttribute(){
        $area_id=$this->region;
        $info=$this->getAreaModel()->newQuery()->where("area_id",$area_id)->first();
        return $info["title"] ?? '';
    }
}